package com.one.controller;

import com.one.pojo.*;
import com.one.service.*;
import com.one.util.EhcacheCacheProvider;
import com.one.util.JSONResult;
import com.one.util.MapUtils;
import com.one.util.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.method.annotation.AbstractJsonpResponseBodyAdvice;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description 好友管理
 * @Author semstouch
 * @Date 2017/4/11
 **/
@Controller
@ControllerAdvice
public class FriendController extends AbstractJsonpResponseBodyAdvice {
    @Resource
    private OneUserService oneUserService;

    @Resource
    private NoticeService noticeService;
    @Resource
    private LandService landService;
    @Resource
    private StoreService storeService;
    @Resource
    private CleanRcordService cleanRcordService;
    @Resource(name="ehcacheCacheProvider")
    private EhcacheCacheProvider cacheProvider;

    public FriendController(){
        super("callback");
    }


    @RequestMapping(value = "/friendList", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult friendList(HttpServletRequest request,String token,String  userId) {
        JSONResult jsonResult = new JSONResult();
        if(!cacheProvider.get(userId).equals(token)){
            jsonResult.setCode("-2");
            jsonResult.setMsg("token失效|Invalid token");
            return jsonResult;
        }
        OneUser oneUser=oneUserService.findByUserId(userId);

        jsonResult.setCode("1");
        jsonResult.setData(oneUserService.findFriend(oneUser.getUserRefId()));
        return  jsonResult;
    }

    @RequestMapping(value = "/enterFriendFarm", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult enterFriendFarm(HttpServletRequest request,String token,String  userId,String friendId) {
        JSONResult jsonResult = new JSONResult();
        if(!cacheProvider.get(userId).equals(token)){
            jsonResult.setCode("-2");
            jsonResult.setMsg("token失效|Invalid token");
            return jsonResult;
        }
        OneUser oneUser=oneUserService.findByUserId(userId);
        OneUser friendUser = oneUserService.findByUserId(friendId);

        jsonResult.setCode("1");
        Map<String, Object> data= MapUtils.beanToMap(friendUser);

        List<Notice> noticeList=noticeService.findAll();
        List<Land> landList = landService.findAll(friendUser.getFarmId());
        Store store = storeService.findByUserId(friendId);

        if(noticeList.size()>1){
            data.put("notice",noticeList.get(0));
        }else{
            data.put("notice",null);
        }

        Integer totalChicken =0;
        if (landList.size()>0)
        {
            data.put("landList",landList);
            for(int i=0;i<landList.size();i++){
                totalChicken+=landList.get(i).getChicksNum();
            }
        }else {
            data.put("landList", new ArrayList());
        }
        if(cleanRcordService.selectByCleanId(userId, StringUtils.getStringToday(), friendId)!=null){
            data.put("isCleanFriend","1");
        }
        data.put("totalEgg",store.getEggsNum());
        data.put("totalChicken",totalChicken+store.getChicksNum().intValue());
        jsonResult.setCode("1");
        jsonResult.setData(data);
        return  jsonResult;
    }
    @RequestMapping(value = "/cleanFriendFarm", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult cleanFriendFarm(HttpServletRequest request,String token,String  userId,String friendId) {
        JSONResult jsonResult = new JSONResult();
        if(!cacheProvider.get(userId).equals(token)){
            jsonResult.setCode("-2");
            jsonResult.setMsg("token失效|Invalid token");
            return jsonResult;
        }
        if(cleanRcordService.selectByCleanId(userId, StringUtils.getStringToday(), friendId)!=null){
            jsonResult.setCode("-1");
            jsonResult.setMsg("已经打扫过了！");
            return jsonResult;
        }
        OneUser oneUser  = oneUserService.findByUserId(userId);
        OneUser friendUser = oneUserService.findByUserId(friendId);
        BigDecimal totalNum = storeService.cleanFriendFarm(oneUser, friendUser);
        jsonResult.setCode("1");
        jsonResult.setData(totalNum);
        return  jsonResult;
    }

    @RequestMapping(value = "/quickClean", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult quickClean(String token,String  userId) {
        JSONResult jsonResult = new JSONResult();
        if(!cacheProvider.get(userId).equals(token)){
            jsonResult.setCode("-2");
            jsonResult.setMsg("token失效|Invalid token");
            return jsonResult;
        }
        OneUser oneUser  = oneUserService.findByUserId(userId);

        List<OneFriend> friendList= oneUserService.findFriend(oneUser.getUserRefId());
        BigDecimal totalNum = new BigDecimal(0);

        for(int i=0;i<friendList.size();i++){
            totalNum=totalNum.add(cleanFarm(userId,friendList.get(i).getUserId()));
        }
        jsonResult.setCode("1");
        jsonResult.setData(totalNum);
        return  jsonResult;
    }

    public BigDecimal cleanFarm(String  userId,String friendId) {
        if(cleanRcordService.selectByCleanId(userId, StringUtils.getStringToday(), friendId)!=null){
            return new BigDecimal(0);
        }
        OneUser oneUser  = oneUserService.findByUserId(userId);
        OneUser friendUser = oneUserService.findByUserId(friendId);
        BigDecimal totalNum = storeService.cleanFriendFarm(oneUser, friendUser);
        return  totalNum;
    }

}
